package com.yk.faceapplication.util;
import java.io.UnsupportedEncodingException;  
import java.security.MessageDigest;  
import java.util.ArrayList;  
import java.util.Collections;  
import java.util.HashMap;
import java.util.List;  
import java.util.Map;  
import org.apache.http.NameValuePair;
import android.annotation.SuppressLint;


public class SignUtils {  
    private static String charsetName = "UTF-8";  
  

    public static String sign(Map<String, String> paramValues, String secret) {  
        return sign(paramValues, null, secret);  
    }  
  

    @SuppressLint("DefaultLocale")
	public static String sign(Map<String, String> paramValues, List<String> ignoreParamNames, String secret) {  
        try {  
            StringBuilder sb = new StringBuilder();  
            List<String> paramNames = new ArrayList<String>(paramValues.size());  
            paramNames.addAll(paramValues.keySet());  
            if (ignoreParamNames != null && ignoreParamNames.size() > 0) {  
                for (String ignoreParamName : ignoreParamNames) {  
                    paramNames.remove(ignoreParamName);  
                }  
            }  
            Collections.sort(paramNames);  
  
            sb.append(secret);  
            for (String paramName : paramNames) {  
                sb.append(paramName).append(paramValues.get(paramName));  
            }  
            sb.append(secret);  
            String md5 =  MD5StringUtil.getMD5(sb.toString());
            return md5.toUpperCase();
//            LogDebugger.error(sb.toString());
//            LogDebugger.error(MD5StringUtil.md5StringFor(sb.toString()));
//            byte[] sha1Digest = getSHA1Digest(sb.toString());  
//            return byte2hex(sha1Digest);  
        } catch (Exception e) {  
            throw new IllegalArgumentException(e.getMessage());  
        }  
    }  
   

    public static String sign(ArrayList<NameValuePair> nameValueList, List<String> ignoreParamNames, String secret) {  
        try {  
        	Map<String, String> paramValues = new HashMap<String, String>();
        	for(NameValuePair nameValuePair:nameValueList){
        		paramValues.put(nameValuePair.getName(), nameValuePair.getValue());
        	}
            StringBuilder sb = new StringBuilder();  
            List<String> paramNames = new ArrayList<String>(paramValues.size());  
            paramNames.addAll(paramValues.keySet());  
            if (ignoreParamNames != null && ignoreParamNames.size() > 0) {  
                for (String ignoreParamName : ignoreParamNames) {  
                    paramNames.remove(ignoreParamName);  
                }  
            }  
            Collections.sort(paramNames);  
  
            sb.append(secret);  
            for (String paramName : paramNames) {  
                sb.append(paramName).append(paramValues.get(paramName));  
            }  
            sb.append(secret);  
            byte[] sha1Digest = getSHA1Digest(sb.toString());  
            return byte2hex(sha1Digest);  
        } catch (Exception e) {  
            throw new IllegalArgumentException(e.getMessage());  
        }  
    }  
    
  
    public static String utf8Encoding(String value, String sourceCharsetName) {  
        try {  
            return new String(value.getBytes(sourceCharsetName), charsetName);  
        } catch (UnsupportedEncodingException e) {  
            throw new IllegalArgumentException(e);  
        }  
    }  

    private static byte[] getSHA1Digest(String data) throws Exception {  
        byte[] bytes = null;  
        MessageDigest md = MessageDigest.getInstance("SHA-1");  
        bytes = md.digest(data.getBytes(charsetName));  
        return bytes;  
    }  
  

    private static String byte2hex(byte[] bytes) {  
        StringBuilder sign = new StringBuilder();  
        for (int i = 0; i < bytes.length; i++) {  
            String hex = Integer.toHexString(bytes[i] & 0xFF);  
            if (hex.length() == 1) {  
                sign.append("0");  
            }  
            sign.append(hex.toUpperCase());  
        }  
        return sign.toString();  
    }  
}  